# encoding=utf-8
# -*- coding: utf-8 -*-
# 识别人脸鉴定是哪个人

# 导入face_recogntion模块，可用命令安装 pip install face_recognition
import face_recognition
from PIL import Image, ImageDraw
#将jpg文件加载到numpy数组中
chen_image = face_recognition.load_image_file(r"D:\est\大伟摄像头.jpg")
myfaceencodeing=face_recognition.face_encodings(chen_image)[0]
#要识别的图片
unknown_image = face_recognition.load_image_file(r"D:\est\秀证件照7.jpg")
unknown_face_encoding = face_recognition.face_encodings(unknown_image)[0]

# 查找图像中所有面部的所有面部特征
facelist=[chen_image,unknown_image]
i=0
for face in facelist:
    i=i+1
    face_landmarks_list = face_recognition.face_landmarks(face)
    print("I found {} face(s) in this photograph.".format(len(face_landmarks_list)))
    for face_landmarks in face_landmarks_list:
        # 打印此图像中每个面部特征的位置
        facial_features = [
            'chin',
            'left_eyebrow',
            'right_eyebrow',
            'nose_bridge',
            'nose_tip',
            'left_eye',
            'right_eye',
            'top_lip',
            'bottom_lip'
        ]

        for facial_feature in facial_features:
            print("The {} in this face has the following points: {}".format(facial_feature, face_landmarks[facial_feature]))

        # 让我们在图像中描绘出每个人脸特征！
        pil_image = Image.fromarray(face)
        d = ImageDraw.Draw(pil_image)

        for facial_feature in facial_features:
            d.line(face_landmarks[facial_feature], width=5)
        #pil_image.show()
        pil_image.save(r"D:\est\大伟证件照711"+str(i)+".jpg")



    #结果是True/false的数组，未知面孔known_faces阵列中的任何人相匹配的结果
    results = face_recognition.compare_faces([myfaceencodeing], unknown_face_encoding)

    print("result :{}".format(results))
    print("这个未知面孔是 陈都灵 吗? {}".format(results[0]))
    print("这个未知面孔是 我们从未见过的新面孔吗? {}".format(not True in results))